package com.erp.erp_ui.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.erp.enums.DictionaryLxEnum;
import com.erp.enums.DictionaryTypeEnum;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_entitys.business.rsp.CompanyLogo;
import com.erp.erp_entitys.warehouse.req.DictionaryInfoReq;
import com.erp.erp_entitys.warehouse.rsp.DictionaryInfoRsp;
import com.erp.erp_servers.Ftp.IFtpService;
import com.erp.erp_ui.base.Base;
import com.erp.erp_ui.business.req.AddOfferReq;
import com.erp.erp_ui.business.req.BatchOperationReq;
import com.erp.erp_ui.business.req.OfferPageListReq;
import com.erp.erp_ui.business.req.OfferProductStatisticsReq;
import com.erp.erp_ui.business.service.OfferService;
import com.erp.erp_ui.business.vo.*;
import com.erp.erp_ui.money.req.OperationReq;
import com.erp.erp_ui.warehouse.vo.AnnexVo;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName : OfferController
 * @Description : 出口业务-报价控制层
 * @Author : lst
 * @Date: 2021-01-14 11:04
 */
@Controller
@RequestMapping(value = "offer")
public class OfferController extends Base {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(OfferController.class);

    /**
     * 报价业务逻辑接口
     */
    private final OfferService offerService;

    private final IFtpService ftpService;
    @Autowired
    public OfferController(OfferService offerService, IFtpService ftpService) {
        this.offerService = offerService;
        this.ftpService = ftpService;
    }

    /**
     * 获取所有国家
     * @return 所有国家
     */
    @GetMapping(value = "getCountry")
    @ResponseBody
    public R getCountry(){
        try {
            LOGGER.info("出口业务-报价-获取国家信息");
            //初始化字段对象
            DictionaryInfoReq dictionaryInfosReq = new DictionaryInfoReq();
            //其它
            dictionaryInfosReq.setType(DictionaryTypeEnum.OTHER.getId());
            //国家
            dictionaryInfosReq.setLx(DictionaryLxEnum.COUNTRY.getId());
            List<DictionaryInfoRsp> dictionaryInfos = offerService.getDictionary(dictionaryInfosReq);
            return R.build(dictionaryInfos);
        }catch (Exception e){
            LOGGER.info("出口业务-报价-获取国家信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取付款方式
     * @return 付款方式
     */
    @GetMapping(value = "getPaymentMethod")
    @ResponseBody
    public R getPaymentMethod(){
        try {
            LOGGER.info("出口业务-报价-获取付款方式");
            //初始化字段对象
            DictionaryInfoReq dictionaryInfosReq = new DictionaryInfoReq();
            //结算相关
            dictionaryInfosReq.setType(DictionaryTypeEnum.SETTLEMENT.getId());
            //付款方式
            dictionaryInfosReq.setLx(DictionaryLxEnum.PAYMENT_METHOD.getId());
            List<DictionaryInfoRsp> dictionaryInfos = offerService.getDictionary(dictionaryInfosReq);
            return R.build(dictionaryInfos);
        }catch (Exception e){
            LOGGER.info("出口业务-报价-获取付款方式异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取验货进度
     * @return 验货进度
     */
    @GetMapping(value = "getInspectionProgress")
    @ResponseBody
    public R getInspectionProgress(){
        try {
            LOGGER.info("出口业务-报价-获取验货进度");
            //初始化字段对象
            DictionaryInfoReq dictionaryInfosReq = new DictionaryInfoReq();
            //结算相关
            dictionaryInfosReq.setType(DictionaryTypeEnum.OTHER.getId());
            //付款方式
            dictionaryInfosReq.setLx(DictionaryLxEnum.INSPECTION_PROGRESS.getId());
            List<DictionaryInfoRsp> dictionaryInfos = offerService.getDictionary(dictionaryInfosReq);
            return R.build(dictionaryInfos);
        }catch (Exception e){
            LOGGER.info("出口业务-报价-获取验货进度异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 首页-分页列表查询
     *
     * @param req 查询参数
     * @return 报价信息
     */
    @GetMapping("/getListByPage")
    @ResponseBody
    public R getListByPage(OfferPageListReq req) {
        try {
            LOGGER.info("首页-分页列表查询 req=" + req.toString());
            PageInfo<OfferPageListVo> pageInfo = offerService.getListByPage(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        } catch (Exception e) {
            LOGGER.info("首页-分页列表查询异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 首页-列表查询
     *
     * @param req 查询参数
     * @return 报价信息
     */
    @GetMapping("/getList")
    @ResponseBody
    public R getList(OfferPageListReq req) {
        try {
            LOGGER.info("首页-列表查询 req=" + req.toString());
            List<OfferPageListVo> list = offerService.getList(req);
            return R.build(list);
        } catch (Exception e) {
            LOGGER.info("首页-列表查询异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取价格术语
     * @return 价格术语
     */
    @GetMapping(value = "getPriceTerms")
    @ResponseBody
    public R getPriceTerms(){
        try {
            LOGGER.info("出口业务-报价-获取价格术语");
            //初始化字段对象
            DictionaryInfoReq dictionaryInfosReq = new DictionaryInfoReq();
            //结算相关
            dictionaryInfosReq.setType(DictionaryTypeEnum.SETTLEMENT.getId());
            //价格术语
            dictionaryInfosReq.setLx(DictionaryLxEnum.PRICE_TERMS.getId());
            List<DictionaryInfoRsp> dictionaryInfos = offerService.getDictionary(dictionaryInfosReq);
            return R.build(dictionaryInfos);
        }catch (Exception e){
            LOGGER.info("出口业务-报价-获取价格术语异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取海关编码
     * @return 海关编码
     */
    @GetMapping(value = "getCustomsCode")
    @ResponseBody
    public R getCustomsCode(){
        try {
            LOGGER.info("出口业务-报价-获取海关编码");
            //初始化字段对象
            DictionaryInfoReq dictionaryInfosReq = new DictionaryInfoReq();
            //商品信息
            dictionaryInfosReq.setType(DictionaryTypeEnum.COMMODITY_INFO.getId());
            //海关编码
            dictionaryInfosReq.setLx(DictionaryLxEnum.CUSTOMS.getId());
            List<DictionaryInfoRsp> dictionaryInfos = offerService.getDictionary(dictionaryInfosReq);
            return R.build(dictionaryInfos);
        }catch (Exception e){
            LOGGER.info("出口业务-报价-获取海关编码异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取运输方式
     * @return 运输方式
     */
    @GetMapping(value = "getModeOfTransport")
    @ResponseBody
    public R getModeOfTransport(){
        try {
            LOGGER.info("出口业务-报价-获取运输方式");
            //初始化字段对象
            DictionaryInfoReq dictionaryInfosReq = new DictionaryInfoReq();
            //基础信息
            dictionaryInfosReq.setType(DictionaryTypeEnum.BASIC_INFORMATION.getId());
            //运输方式
            dictionaryInfosReq.setLx(DictionaryLxEnum.MODE_OF_TRANSPORT.getId());
            List<DictionaryInfoRsp> dictionaryInfos = offerService.getDictionary(dictionaryInfosReq);
            return R.build(dictionaryInfos);
        }catch (Exception e){
            LOGGER.info("出口业务-报价-获取运输方式异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取货柜型号
     * @return 货柜型号
     */
    @GetMapping(value = "getContainerModel")
    @ResponseBody
    public R getContainerModel(){
        try {
            LOGGER.info("出口业务-报价-获取货柜型号");
            //初始化字段对象
            DictionaryInfoReq dictionaryInfosReq = new DictionaryInfoReq();
            //基础信息
            dictionaryInfosReq.setType(DictionaryTypeEnum.BASIC_INFORMATION.getId());
            //货柜型号
            dictionaryInfosReq.setLx(DictionaryLxEnum.CONTAINER_MODEL.getId());
            List<DictionaryInfoRsp> dictionaryInfos = offerService.getDictionary(dictionaryInfosReq);
            return R.build(dictionaryInfos);
        }catch (Exception e){
            LOGGER.info("出口业务-报价-获取货柜型号异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取费用类型
     * @return 费用类型
     */
    @GetMapping(value = "getCostType")
    @ResponseBody
    public R getCostType(){
        try {
            LOGGER.info("出口业务-报价-获取费用类型");
            //初始化字段对象
            DictionaryInfoReq dictionaryInfosReq = new DictionaryInfoReq();
            //结算相关
            dictionaryInfosReq.setType(DictionaryTypeEnum.SETTLEMENT.getId());
            //费用类型
            dictionaryInfosReq.setLx(DictionaryLxEnum.COST_TYPE.getId());
            List<DictionaryInfoRsp> dictionaryInfos = offerService.getDictionary(dictionaryInfosReq);
            return R.build(dictionaryInfos);
        }catch (Exception e){
            LOGGER.info("出口业务-报价-获取费用类型异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取起运地、目的地
     * @return 起运地、目的地
     */
    @GetMapping(value = "getTransitPlace")
    @ResponseBody
    public R getTransitPlace(){
        try {
            LOGGER.info("出口业务-报价-获取起运地、目的地");
            //初始化字段对象
            DictionaryInfoReq dictionaryInfosReq = new DictionaryInfoReq();
            //其它
            dictionaryInfosReq.setType(DictionaryTypeEnum.OTHER.getId());
            //起运地、目的地
            dictionaryInfosReq.setLx(DictionaryLxEnum.TRANSIT_PLACE.getId());
            List<DictionaryInfoRsp> dictionaryInfos = offerService.getDictionary(dictionaryInfosReq);
            return R.build(dictionaryInfos);
        }catch (Exception e){
            LOGGER.info("出口业务-报价-获取起运地、目的地异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取业务模式
     * @return 业务模式
     */
    @GetMapping(value = "getBusinessModel")
    @ResponseBody
    public R getBusinessModel(){
        try {
            LOGGER.info("出口业务-报价-获取业务模式");
            //初始化字段对象
            DictionaryInfoReq dictionaryInfosReq = new DictionaryInfoReq();
            //其它
            dictionaryInfosReq.setType(DictionaryTypeEnum.OTHER.getId());
            //业务模式
            dictionaryInfosReq.setLx(DictionaryLxEnum.BUSINESS_MODEL.getId());
            List<DictionaryInfoRsp> dictionaryInfos = offerService.getDictionary(dictionaryInfosReq);
            return R.build(dictionaryInfos);
        }catch (Exception e){
            LOGGER.info("出口业务-报价-获取业务模式异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 新增、修改
     * @param req 新增、修改入参
     * @return 新增、修改结果
     */
    @PostMapping("/add")
    @ResponseBody
    public R add(@Validated @RequestBody AddOfferReq req){
        try {
            LOGGER.info("出口业务-报价-新增、修改 req=" + req.toString());
            UserEntity user = (UserEntity)this.getBean("user");
            String name = user.getName();
            String code = user.getCode();
            return offerService.add(req, name, code);
        }catch (Exception e){
            LOGGER.info("出口业务-报价-新增、修改异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 详情
     * @param id 详情id
     * @return 报价详情信息
     */
    @GetMapping("/detail")
    @ResponseBody
    public R detail(Integer id){
        try {
            LOGGER.info("出口业务-报价-详情：id=" + id);
            OfferDetailVo detailVo = offerService.detail(id);
            return R.build(detailVo);
        }catch (Exception e){
            LOGGER.info("出口业务-报价-详情异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 出口业务-删除
     * @param req 删除条件
     * @return 删除结果
     */
    @PostMapping("/delete")
    @ResponseBody
    public R delete(@RequestBody OperationReq req) {
        try {
            LOGGER.info("出口业务-删除 req=" + req);
            offerService.delete(req);
            return R.build("");
        } catch (Exception e) {
            LOGGER.info("出口业务-删除异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }

    /**
     * 数据操作接口
     * 1.提交审批 2.审批通过 3.审批退回 4.完成 5.还原
     * @param req 操作类型
     * @return 操作结果
     */
    @PostMapping("/operation")
    @ResponseBody
    public R operation(@RequestBody OperationReq req) {
        try {
            LOGGER.info("出口业务-报价-数据操作接口 req=" + req);
            return offerService.operation(req);
        } catch (Exception e) {
            LOGGER.info("出口业务-报价-数据操作接口异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 数据批量操作接口
     * @param req 操作类型
     * @return 操作结果
     */
    @PostMapping("/batchOperation")
    @ResponseBody
    public R batchOperation(@RequestBody BatchOperationReq req) {
        try {
            LOGGER.info("出口业务-报价-数据批量操作 req=" + req);
            UserEntity user = (UserEntity)this.getBean("user");
            return offerService.batchOperation(req, user.getMsn());
        } catch (Exception e) {
            LOGGER.info("出口业务-报价-数据批量操作异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 根据产品规格编码获取对应配件信息
     * @param specNo 产品规格编码
     * @param productCode 产品编码
     * @return 对应配件信息
     */
    @GetMapping("/getProductPartsInfoBySpecNo")
    @ResponseBody
    public R getProductPartsInfoBySpecNo(String specNo, String productCode) {
        try {
            LOGGER.info("出口业务-报价-根据产品规格编码获取对应配件信息 specNo=" + specNo + ",productCode=" + productCode);
            List<ProductPartsInfoVo> infoVos = offerService.getProductPartsInfoBySpecNo(specNo, productCode);
            return R.build(infoVos);
        } catch (Exception e) {
            LOGGER.info("出口业务-报价-根据产品规格编码获取对应配件信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 根据产品编码获取对应附件信息
     * @param productCode 产品编码
     * @return 获取对应附件信息
     */
    @GetMapping("/getAnnexByProductCode")
    @ResponseBody
    public R getAnnexByProductCode(String productCode) {
        try {
            LOGGER.info("出口业务-报价-根据产品编码获取对应附件信息: productCode=" + productCode);
            List<AnnexVo> voList = offerService.getAnnexByProductCode(productCode);
            return R.build(voList);
        } catch (Exception e) {
            LOGGER.info("出口业务-报价-根据产品编码获取对应附件信息异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 统计
     * @return 统计信息
     */
    @GetMapping(value = "statistics/getOfferProductStatisticsByPage")
    @ResponseBody
    public R getOfferProductStatistics(OfferProductStatisticsReq req){
        try {
            LOGGER.info("出口业务-报价-统计");
            PageInfo<OfferProductStatisticsVo> pageInfo = offerService.getOfferProductStatistics(req);
            return R.build(0, "", pageInfo.getTotal(), pageInfo.getList());
        }catch (Exception e){
            LOGGER.info("出口业务-报价-统计异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 统计-导出Excel
     * @return 统计信息
     */
    @GetMapping(value = "statistics/export/getOfferProductStatisticsInfo")
    @ResponseBody
    public R getOfferProductStatisticsInfo(OfferProductStatisticsReq req){
        try {
            LOGGER.info("出口业务-报价-统计-导出Excel");
            JSONObject obj = offerService.getOfferProductStatisticsInfo(req);
            return R.build(obj);
        }catch (Exception e){
            LOGGER.info("出口业务-报价-统计-导出Excel异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }


    /**
     * 根据参数返回logo的url
     * @param id
     * @return
     */
    @GetMapping("/getImgLogoUrl")
    @ResponseBody
    public R getImgLogoUrl(Integer id){
        try {
            LOGGER.info("出口业务-报价-根据参数返回logo的url");
            CompanyLogo companyLogo = offerService.getImgLogoUrl(id);
            if(companyLogo.getcLogo()!=null){
                ftpService.ftpUpload(companyLogo.getcLogo());
            }
            return R.build(companyLogo);
        }catch (Exception e){
            LOGGER.info("出口业务-报价-根据参数返回logo的url" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取本月报价总额USD
     * @return 本月报价总额USD
     */
    @GetMapping("/statistics/getOfferTotalContractAmount")
    @ResponseBody
    public R getOfferTotalContractAmount(){
        try {
            LOGGER.info("出口业务-报价-获取本月报价总额USD");
            UserEntity user = (UserEntity)this.getBean("user");
            String name = user.getName();
            String code = user.getCode();
            BigDecimal count = offerService.getOfferTotalContractAmount(name, code);
            return R.build(count);
        }catch (Exception e){
            LOGGER.info("出口业务-报价-获取本月报价总额USD异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取新的报价单号
     * @return 新的报价单号
     */
    @GetMapping("/getOfferBillNo")
    @ResponseBody
    public R getOfferBillNo(){
        try {
            LOGGER.info("出口业务-报价-获取新的报价单号");
            String billNo = offerService.getOfferBillNo();
            return R.build(billNo);
        }catch (Exception e){
            LOGGER.info("出口业务-报价-获取新的报价单号异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 出口业务-报价-修改标记颜色
     *
     * @param req 入参实体
     * @return 修改结果
     */
    @PostMapping("/updateMarkColor")
    @ResponseBody
    public R updateMarkColor(@RequestBody OperationReq req) {
        try {
            LOGGER.info("出口业务-报价-修改标记颜色 req=" + req);
            offerService.updateMarkColor(req);
            return R.build("");
        } catch (Exception e) {
            LOGGER.info("出口业务-报价-修改标记颜色异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取备选供应商
     *
     * @param supplierCode 供应商编码
     * @return 获取备选供应商
     */
    @GetMapping("/getAlternativeSupplierList")
    @ResponseBody
    public R getAlternativeSupplierList(String supplierCode) {
        try {
            LOGGER.info("出口业务-报价-获取备选供应商 supplierCode=" + supplierCode);
            List<AlternativeSupplierVo> voList = offerService.getAlternativeSupplierList(supplierCode);
            return R.build(voList);
        } catch (Exception e) {
            LOGGER.info("出口业务-报价-获取备选供应商异常:" + e);
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }
    }

    /**
     *
     * 判断该用户是否拥有 报价查看详情页面上传功能 角色
     */
    @RequestMapping("isContainsRoleOffer")
    @ResponseBody
    public R isContainsRoleOffer(){
        try {
            UserEntity user = (UserEntity) this.getBean("user");
            if (user.getRoles().contains("BJCKXQYMSCGN")){
                return R.build(true);
            }else {
                return R.build(false);
            }
        } catch (Exception e) {
            return R.buildError(ResultStatusEnum.SYSTEM_ERROR);
        }

    }

}
